package com.ctgu.zmc.stresstest.controller;

import com.ctgu.zmc.stresstest.common.utils.PageParams;
import com.ctgu.zmc.stresstest.common.utils.PageResult;
import com.ctgu.zmc.stresstest.common.utils.Result;
import com.ctgu.zmc.stresstest.entity.BlogEntity;
import com.ctgu.zmc.stresstest.service.BlogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author zmc
 * @description 博客管理接口
 * @date 2023-05-28 16:09:05
 */
@RestController
@RequestMapping("/blog")
@Api(value = "博客管理接口", tags = "博客管理接口")
public class BlogController {

    private final BlogService blogService;

    public BlogController(BlogService blogService) {
        this.blogService = blogService;
    }

    @ApiOperation("按博客id查询博客信息--- 通过MySQL")
    @GetMapping("/info/{id}")
    public Result getBlogById(@PathVariable("id") Long id) {
        BlogEntity blogEntity = blogService.getBlogById(id);
        return Result.ok().put("blog", blogEntity);
    }

    @ApiOperation("按博客id查询博客信息---通过redis")
    @GetMapping("/info/redis/{id}")
    public Result getBlogByIdWithRedis(@PathVariable("id") Long id) {
        BlogEntity blogEntity = blogService.getBlogByIdWithRedis(id);
        return Result.ok().put("blog", blogEntity);
    }

    @ApiOperation("分页检索博客信息---MySQL模糊查询")
    @PostMapping("/query/page/{key}")
    public Result queryPage(@PathVariable("key") String key, @RequestBody(required = false) @Validated PageParams pageParams) {
        PageResult<BlogEntity> pageResult = blogService.queryPage(key, pageParams);
        return Result.ok().put("pageResult", pageResult);
    }

    @ApiOperation("分页检索博客信息---Elasticsearch全文检索")
    @PostMapping("/query/page/es/{key}")
    public Result queryPageWithElasticsearch(@PathVariable("key") String key, @RequestBody(required = false) @Validated PageParams pageParams) {
        PageResult<BlogEntity> pageResult = blogService.queryPageWithElasticsearch(key, pageParams);
        return Result.ok().put("pageResult", pageResult);
    }

}
